<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.status === '0'" class="tag">默认</text>
					<text class="address">{{item.pro_city}} {{item.addressdetail}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.username_address}}</text>
					<text class="mobile">{{item.account_address}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
			<image class="edit" src="../../static/images/edit.png" @click.stop="editaddress(item)"></image>
		</view>
	
		<button class="add-btn" @click="addAddress('add')">+新增地址</button>
	</view>
</template>

<script>
	//渲染所有地址信息
	import { _selectaddress} from '@/service/shop.js'
	export default {
		data() {
			return {
				source: 0,
				addressList: [],
				accountNum: null
			}
		},
		onLoad(option){
			console.log(option.source);
			this.source = option.source;
		},
		created(){
			let newdata;
			let self = this;
			uni.showLoading({
				title: "请稍后"
			});
			try {
				const value = uni.getStorageSync('userInfo');
				console.log(value)
				if (value) {
					newdata = value;
					if (newdata != null){
						{
							this.accountNum=newdata.accountNum
						}
					}
				}
			} catch (e) {
				console.error(e);
			}
			this.getAllAddress().
			then(function(data) {
					//异步操作，成功返回将执行下列操作
					console.log(data)
					self.addressList = data; //保存返回值，然后页面就更新啦
					uni.hideLoading();//关闭加载叠层
				}).
				catch(function(data) {
					console.log(data); //失败则输出
					uni.hideLoading();//关闭加载叠层
				})
		},
		methods: {
			//查询用户所有收货地址
			async getAllAddress(){
				let result;
				try {
					result = await _selectaddress({
					account_user:this.accountNum
				});
				} catch (error) {
					throw error; //抛出异常
				}
				if (result == undefined || result != undefined && result.status != 200) {
					throw new Error("接口返回异常，位置address.vue");//接口调用但返回仍异常,则抛出
				}
				return result.data;
			},
			
			
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					console.log(item);
					this.$api.prePage().addressData = item;
					uni.navigateBack()
				}
			},
			addAddress(type, item){
				let data = {};
				if (type === 'edit') {
					data = {
						id: item.id || '',
						receiveName: item.receiveName,
						receivePhone: item.receivePhone,
						pcaName: item.pcaName,
						pcaName_page: item.pcaName_page,
						pcaId: item.pcaId,
						zipCode: item.zipCode,
						detailAddress: item.detailAddress,
						default: item.status == '0' || false
					};
				}
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(data)}`
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);
				
				console.log(data, type);
			},
			
			editaddress(data) {
			  console.log(data);
			  // 显示加载动画
			  uni.showLoading({
			    title: '加载中...'
			  });
			
			  // 异步保存数据到本地缓存
			  uni.setStorage({
			    key: 'editaddress',
			    data: data,
			    success: () => {
			      // 隐藏加载动画
			      uni.hideLoading();
			
			      // 跳转到目标界面
			      uni.navigateTo({
			        url: '/pages/address/editaddress'
			      });
			    },
			    fail: (error) => {
			      console.log('Failed to save data:', error);
			      // 隐藏加载动画并显示错误信息
			      uni.hideLoading();
			      uni.showToast({
			        title: '保存数据失败',
			        icon: 'none'
			      });
			    }
			  });
			}

		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 100upx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			/* color: $base-color; */
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		/* .address{
			font-size: 30upx;
			color: $font-color-dark;
		} */
	}
	.u-box{
		font-size: 28upx;
		/* color: $font-color-light; */
		margin-top: 16upx;
		.name{
			margin-right: 30upx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		/* color: $font-color-light; */
		padding-left: 30upx;
	}
	
	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: #138f87;
		border-radius: 56.25rpx;
	}
	.edit{
		width: 50rpx;
		height: 50rpx;
		margin-right: -20rpx;
	}
</style>
